<?php
/**
 * 常用工具函数，程序会自动加载
 * 
 * @author Lee  <a605333742@gmail.com>
 * @time    2016-07-20
 */

/*
 * 异位或加密解密
 * 
 * @param   $value  加密的字符
 * @param   $type   标志，true为加密，false为解密，默认为解密
 * 
 * $return  str
 */
function code($value,$type=''){
    $key    =md5(C('AUTO_LOGIN_KEY'));
    if($type){
        //加密
        return str_replace("=",'',base64_encode($value ^ $key));
    }
    //解密
    $value	=base64_decode($value);
    return  $value^$key;
}

/*
 * 根据节点id获取节点名
 * 
 * @param   $node_id    节点id
 * 
 * @return  str $nodeName
 */
function getNodeName($node_id=''){
    if($node_id=="" ||$node_id==0){
        return "顶级节点";
    }
    //if($nodeName=S("nodeName")){
    //    return $nodeName[$node_id];
    //}
    $node       =D("Node");
    $nodeList   =$node->select(array('field' => 'id,name'));
    $data       =array();
    foreach($nodeList as $vo){
        $data[$vo['id']]=$vo['name'];
    }
    //S("nodeName",$data,3600*24);
    $nodeName   =$data[$node_id];    
    return $nodeName;
}

/*
 * 根据id查询用户角色
 * 
 * @param user_id   用户id
 * 
 * @return $rName
 */
function getUserRole($user_id=''){
    if($user_id==''){
        return '';
    }
    $u_role =D("UserRole");
    $role_id=$u_role->where(array("u_id"=>$user_id))->getField("role_id");
    $rName  =D("Role")->where(array("id"=>$role_id))->getField('name');
    return $rName;
}

/*
 * 获取用户名
 * 
 * @return $userName
 */
function getUsername($user_id=''){
    if($user_id==''){
        return "";
    }
    $user       =D("User");
    $userList   =$user->select(array('field' => 'id,name'));
    $data       =array();
    foreach($userList as $vo){
        $data[$vo['id']]=$vo['name'];
    }
    $userName   =$data[$user_id];
    return $userName;
}
/*
 * 判断目录是否为空
 * 
 * @param	string	$ddir	指定目录
 * @param	bool	$path	删除指定目录还是删除指定目录下的所有子目录
 * 
 * @return  void
*/
function is_empty_dir($ddir){
    $d=opendir($ddir);
    $i=0;
    while($a=readdir($d)){
            $i++;
    }
    closedir($d);
    if($i>2){return false;}
    else {return true;}
}


/*
 * 递归删除目录
 * 
 * @param $dir 指定要删除的目录
 * 
 * @return #
 */
function deldir($dir) {
    $dh = opendir($dir);
    while ($file = readdir($dh)) {
        if ($file != "." && $file != "..") {
            $fullpath = $dir . "/" . $file;
            if (!is_dir($fullpath)) {
                unlink($fullpath);
            } else {
                deldir($fullpath);
            }
        }
    }
}

/*
 * 根据状态显示相应的图片
 * 
 * @return  #
 */
function getStatus($status, $imageShow = true){
    switch ($status) {
        case 0 :
           $showText = '删除';
            $showImg = '<IMG SRC="/mhsoft_rbac/Application/Public/Admin/img/del.gif" style="height: 20px; width: 20px;" BORDER="0" ALT="删除">';
            break;
        case 1 :
            $showText = '正常';
            $showImg = '<IMG SRC="/mhsoft_rbac/Application/Public/Admin/img/ok.gif" style="height: 20px; width: 20px;" BORDER="0" ALT="正常">';
            break;
        case 2 : 
             $showText = '禁用';
            $showImg = '<IMG SRC="/mhsoft_rbac/Application/Public/Admin/img/locked.gif" style="height: 20px; width: 20px;" BORDER="0" ALT="禁用">';
        break;  
    }
    return ($imageShow === true) ? $showImg : $showText;
}









